package lian_xi;

import java.util.concurrent.TimeUnit;

public class Exchanger {
    public static void main(String[] args) {
        int repeat  = 2;
        Exchanger exc = new Exchanger();
        new Thread(()-> {
            for (int i = 0; i < repeat; i++) {
                String info  = null;
                if(i % 2 == 0){
                    info = "A";
                }
                else{
                    info = "B";
                }
                try {
                    TimeUnit.SECONDS.sleep(1);
                    exc.exchange(info);
                    System.out.println(Thread.currentThread().getName() + ": " + info);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"信息生产者").start();
        new Thread(()-> {
            for (int i = 0; i < repeat; i++) {
                try{
                    TimeUnit.SECONDS.sleep(1);
                    String info = exc.exchange(null);
                    System.out.println(Thread.currentThread().getName() + ": " + info);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        },"信息消费者").start();
    }

    private String exchange(String info) {
        return info;
    }
}
